package com.yan.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.yan.Junt.junt;
import com.yan.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@org.springframework.stereotype.Controller
@RestController
public class ControllerTest {

    // @RequestMapping(value = "/st1",produces="application/json;charset=utf-8")
    @RequestMapping(value = "/st1")
    @ResponseBody
    public String st1() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        User user = new User("闫文超", 14, "难");

        String s = mapper.writeValueAsString(user);

        return s;
    }


    @RequestMapping(value = "/st2")
    @ResponseBody
    public String st2() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();


        List<User> list = new ArrayList<>();

        User user = new User("闫文超1", 14, "难");
        User user1 = new User("闫文超2", 14, "难");
        User user2 = new User("闫文超3", 14, "难");
        User user3 = new User("闫文超4", 14, "难");
        User user4 = new User("闫文超5", 14, "难");

        list.add(user);
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);


        String s = mapper.writeValueAsString(list);

        return s;
    }


    @RequestMapping(value = "/st3")
    @ResponseBody
    public String st3() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();


        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = mapper.writeValueAsString(format.format(date));

        return s;
    }



    @RequestMapping(value = "/st4")
    @ResponseBody
    public String st4() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mapper.setDateFormat(format);
        Date date = new Date();
        String s = mapper.writeValueAsString(date);

        return s;
    }


    @RequestMapping(value = "/st5")
    @ResponseBody
    public String st5() throws JsonProcessingException {

        Date date = new Date();
        return junt.getsong(date,"yyyy-MM-dd HH:mm:ss");
    }







    @RequestMapping(value = "/st6")
    @ResponseBody
    public String st6() throws JsonProcessingException {
        List<User> list = new ArrayList<>();

        User user = new User("闫文超1", 14, "难");
        User user1 = new User("闫文超2", 14, "难");
        User user2 = new User("闫文超3", 14, "难");
        User user3 = new User("闫文超4", 14, "难");
        User user4 = new User("闫文超5", 14, "难");

        list.add(user);
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);


        return junt.getsong(list);
    }


    @RequestMapping(value = "/st7")
    @ResponseBody
    public void st7() throws JsonProcessingException {
        List<User> list = new ArrayList<>();

        User user = new User("闫文超1", 14, "难");
        User user1 = new User("闫文超2", 14, "难");
        User user2 = new User("闫文超3", 14, "难");
        User user3 = new User("闫文超4", 14, "难");
        User user4 = new User("闫文超5", 14, "难");

        list.add(user);
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);

        System.out.println(user);//User(name=闫文超1, age=14, sex=难)
        System.out.println(user1);
        System.out.println("list"+list);//list[User(name=闫文超1, age=14, sex=难), User(name=闫文超2, age=14, sex=难), User(name=闫文超3, age=14, sex=难),
        // User(name=闫文超4, age=14, sex=难), User(name=闫文超5, age=14, sex=难)]
/**
 * *******Java对象 转 JSON字符串*******
 * JSON.toJSONString(list)==>[{"age":14,"name":"闫文超1","sex":"难"},{"age":14,"name":"闫文超2","sex":"难"},{"age":14,"name":"闫文超3","sex":"难"},{"age":14,"name":"闫文超4","sex":"难"},{"age":14,"name":"闫文超5","sex":"难"}]
 * JSON.toJSONString(user1)==>{"age":14,"name":"闫文超2","sex":"难"}
 */

        System.out.println("*******Java对象 转 JSON字符串*******");
        String str1 = JSON.toJSONString(list);
        System.out.println("JSON.toJSONString(list)==>"+str1);
        String str2 = JSON.toJSONString(user1);
        System.out.println("JSON.toJSONString(user1)==>"+str2);
//* ****** JSON字符串 转 Java对象*******
// * JSON.parseObject(str2,User.class)==>User(name=闫文超2, age=14, sex=难)

        System.out.println("\n****** JSON字符串 转 Java对象*******");
        User jp_user1=JSON.parseObject(str2,User.class);
        System.out.println("JSON.parseObject(str2,User.class)==>"+jp_user1);

//         * ****** Java对象 转 JSON对象 ******
// * (JSONObject) JSON.toJSON(user2)==>闫文超3


        System.out.println("\n****** Java对象 转 JSON对象 ******");
        JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
        System.out.println("(JSONObject) JSON.toJSON(user2)==>"+jsonObject1.getString("name"));
        //{"sex":"难","name":"闫文超3","age":14}
        System.out.println("(JSONObject) JSON.toJSON(user2)==1111>"+jsonObject1.toString());


// * ****** JSON对象 转 Java对象 ******
// * JSON.toJavaObject(jsonObject1, User.class)==>User(name=闫文超3, age=14, sex=难

        System.out.println("\n****** JSON对象 转 Java对象 ******");
        User to_java_user = JSON.toJavaObject(jsonObject1, User.class);
        System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>"+to_java_user);
    }














}
